package models

import (
	"gorm.io/gorm"
)

// 用户表
type User struct {
	gorm.Model
	Mobile          string `gorm:"type:varchar(11);comment:'手机号'"`
	Password        string `gorm:"type:varchar(100);comment:'密码'"`
	Nickname        string `gorm:"type:varchar(30);comment:'昵称'"`
	MemberStatus    int    `gorm:"type:tinyint;default:1;comment:'会员状态 1:普通 2:会员'"`
	MemberExpiredAt string `gorm:"varchar(30);comment:'会员有效期'"`
}

func (u *User) GetUserById(db *gorm.DB, id int) error {
	return db.Where("id = ?", id).Limit(1).Find(&u).Error
}

// 视频剧集播放记录
type EpisodePlayRecord struct {
	gorm.Model
	VideoId   int64 `gorm:"type:int;not null;comment:视频ID"`
	EpisodeId int64 `gorm:"type:int;not null;comment:视频集数ID"`
	UserId    int64 `gorm:"type:int;not null;comment:审核员ID"`
	Seconds   int64 `gorm:"type:int;default:0;comment:观看时长"`
}

// 增加播放记录
func (e *EpisodePlayRecord) AddPlayRecord(db *gorm.DB) error {
	return db.Create(&e).Error
}

// 查看剧集播放,是某人某个视频集数的播放
func (e *EpisodePlayRecord) GetEpisodePlayRecord(db *gorm.DB,
	userId, episodeId int) error {

	return db.Where("user_id = ?", userId).
		Where("episode_id = ?", episodeId).Limit(1).Find(&e).Error
}

// 查询视频集数存在
func (e *Episode) ExistEpisode(db *gorm.DB, videoId int,
	episodeNo int) (exist bool, err error) {

	var count int64
	err = db.Where("video_id = ?", videoId).
		Where("episode_no = ?", episodeNo).Count(&count).Error
	return count > 0, err
}

// 剧集视频关联表
type Correlated struct {
	gorm.Model
	EpisodeId int64  //电视剧ID
	VideoId   int64  //分集ID
	NumNo     string //第几集
}

func (u *Correlated) CreateCorrelated(db *gorm.DB) error {
	return db.Create(&u).Error
}

//获取视频集数详情
//func (e *Episode) GetEpisodeDetail(db *gorm.DB,
//	episodeId int) (episode *__.EpisodeDetailResp, err error) {
//
//	err = db.Select("id,title,url,episode_no,seconds").
//		Where("id =?", episodeId).Limit(1).Find(&episode).Error
//	return episode, err
//}

// Series 剧集信息结构体
//type Series struct {
//	gorm.Model
//	Name      string `gorm:"size:255;not null"`
//	PlayCount int64  `gorm:"default:0"`
//}
//
//// SeriesVideo 剧集与视频的关联关系
//type SeriesVideo struct {
//	gorm.Model
//	ID       int64 `gorm:"primaryKey"`
//	SeriesID int64 `gorm:"not null;index"`
//	VideoID  int64 `gorm:"not null;index"`
//	Chapter  int   `gorm:"not null"` // 章节编号
//}

// VideoStatus 视频状态枚举
//type VideoStatus int
//
//const (
//	VideoStatusPendingReview VideoStatus = iota // 待审核
//	VideoStatusInReview                         // 审核中
//	VideoStatusApproved                         // 通过
//	VideoStatusRejected                         // 驳回
//)
//
//// String 转换为字符串描述
//func (s VideoStatus) String() string {
//	switch s {
//	case VideoStatusPendingReview:
//		return "待审核"
//	case VideoStatusInReview:
//		return "审核中"
//	case VideoStatusApproved:
//		return "通过"
//	case VideoStatusRejected:
//		return "驳回"
//	default:
//		return "未知状态"
//	}
//}
//
//// StatusCodeToVideoStatus 将状态码转换为VideoStatus
//func StatusCodeToVideoStatus(code int) (VideoStatus, error) {
//	switch code {
//	case 0:
//		return VideoStatusPendingReview, nil
//	case 1:
//		return VideoStatusInReview, nil
//	case 2:
//		return VideoStatusApproved, nil
//	case 3:
//		return VideoStatusRejected, nil
//	default:
//		return 0, fmt.Errorf("无效的状态码: %d", code)
//	}
//}
//
//// ConvertReviewStatusToVideoStatus 将gRPC的ReviewStatus转换为本地VideoStatus
//func ConvertReviewStatusToVideoStatus(grpcStatus int32) VideoStatus {
//	switch grpcStatus {
//	case 0:
//		return VideoStatusPendingReview
//	case 1:
//		return VideoStatusInReview
//	case 2:
//		return VideoStatusApproved
//	case 3:
//		return VideoStatusRejected
//	default:
//		return VideoStatusPendingReview
//	}
//}
